﻿using System;

namespace TeF.Runner
{
    internal abstract class TestStateEntityBase : TestEntityBase, ITestEntity
    {
        protected TestStateEntityBase(bool ignored)
            : base(ignored)
        {
            Reset();
        }

        public TestState State { get; private set; }
        public string Log { get; private set; }
        
        public event StateChangedEventHandler StateChanged;
        public event LogChangedEventHandler LogChanged;
        
        public void Reset()
        {
            SetState(Ignored ? TestState.Skipped : TestState.Loaded);
            Log = string.Empty;
        }

        public void SetState(TestState state)
        {
            if (State != state)
            {
                State = state;
                if (StateChanged != null)
                {
                    var eventArgs = new StateChangedEventArgs(state);
                    StateChanged(this, eventArgs);
                }
            }
        }

        public void SetLog(string log)
        {
            if (string.IsNullOrWhiteSpace(log))
                throw new ArgumentException("log");

            if (Log != log)
            {
                Log = log;
                if (LogChanged != null)
                {
                    var eventArgs = new LogChangedEventArgs(log);
                    LogChanged(this, eventArgs);
                }
            }
        }
    }
}
